Component({
  properties: {
    src: {
      type: String,
      value: ''
    },
    preSrc: {
      type: String,
      value: ''
    },
    errSrc: {
      type: String,
      value: ''
    },
    mode: {
      type: String,
      // value: 'widthFix'
    },
    width: {
      type: String,
      value: ""
    },
    height: {
      type: String,
      value: ""
    },
    radius: {
      type: String,
      value: "0"
    },
    shadow: {
      type: String,
      value: 'none'
    }
  },
  attached() {
    this.loadErrFlag = 0;
    if ( !this.data.src ) {
      this.setData({
        src: this.data.errSrc
      })
    }
  },
  methods: {
    /**
     * 图片加载完毕
     */
    onImgLoaded() {
      let preSrc = this.data.preSrc;
      // 如果预览图片存在，则删除预览图片
      if ( preSrc ) {
        this.setData({
          preSrc: ''
        })
      }
    },
    onImgLoadError() {
      console.log('on image load error.');
      let errSrc = this.data.errSrc;
      let preSrc = this.data.preSrc;
      // 如果替补图片加载失败，则不再加载图片，防止死循环
      if ( this.loadErrFlag > 1 ) {
        this.setData({
          src: ''
        })
      }
      // 如果图片加载失败，则使用替补图片
      if ( errSrc &&  preSrc !== errSrc) {
        this.setData({
          src: errSrc,
          preSrc: ''
        })
      }
      this.loadErrFlag ++;
    }
  }
}) 